package com.yzy.pe.controller;

import com.github.pagehelper.PageInfo;
import com.yzy.pe.entity.User;
import com.yzy.pe.entity.UserWj;
import com.yzy.pe.entity.WeakCheck;
import com.yzy.pe.entity.dto.NameValueDto;
import com.yzy.pe.service.AdminService;
import com.yzy.pe.service.TeacherService;
import com.yzy.pe.service.UserService;
import com.yzy.pe.util.DateUtil;
import com.yzy.pe.util.ImportExcelUtil;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@EnableAutoConfiguration
@RequestMapping("/admin")
public class AdminController {

    @Resource
    private TeacherService teacherService;

    @Resource
    private AdminService adminService;

    @Resource
    private UserService userService;

    /**
     * Description 打开所有学生页面
     */
    @RequestMapping("/all_user")
    public ModelAndView allUser() {
        ModelAndView mv = new ModelAndView("/admin/all_user");
        mv.addObject("isSuccess","3");
        return mv;
    }

    /**
     * Description 所有学生信息
     */
    @RequestMapping("/getUserList")
    @ResponseBody
    public PageInfo<User> getDeletePoint(String userId, @RequestParam(defaultValue = "1") int pageNum,
                                         @RequestParam(defaultValue = "10") int pageSize) {
        User user = new User();
        user.setUserId(userId);
        List<User> list = userService.selectUserList(user, pageNum, pageSize);
        return new PageInfo<>(list);
    }

    /**
     * Description 打开学生详情页面
     */
    @GetMapping("/getUser")
    public ModelAndView getUser(String userId) {
        ModelAndView mv = new ModelAndView("/admin/user_msg");
        User user = new User();
        user.setUserId(userId);
        User user1 = userService.selectUser(user);
        mv.addObject("user",user1);
        return mv;
    }

    /**
     * Description 打开周信息页面
     */
    @RequestMapping("/week")
    public ModelAndView week() {
        ModelAndView mv = new ModelAndView("/admin/week");
        return mv;
    }

    @RequestMapping("/initSelect")
    @ResponseBody
    public Map<String, List> initSelect() {
        return adminService.initSelect();
    }

    @RequestMapping("/getWeekCheck2")
    @ResponseBody
    public List<WeakCheck> getWeekCheck2() {
        return adminService.getWeek2();
    }

    @RequestMapping("/getWeekData")
    @ResponseBody
    public Map<String, List> getWeekData(String xqs, String weekNum, String qdbm) {
        return adminService.getWeekData(xqs, weekNum, qdbm);
    }

    @RequestMapping("/getWeekData2")
    @ResponseBody
    public Map<String, List> getWeekData2(String xqs, String weekNum) {
        return adminService.getWeekData2(xqs, weekNum);
    }

    /**
     * Description 人数
     */
    @RequestMapping("/stuCount")
    @ResponseBody
    public List<String> stuCount() {
        String qrrkCount = adminService.stuCount();
        List<String> countList = new ArrayList<>();
        if (!StringUtils.isEmpty(qrrkCount)) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < 5 - qrrkCount.length(); i++) {
                sb.append("0");
            }
            sb.append(qrrkCount);
            String qrrkStr = sb.toString();
            countList.add(qrrkStr.substring(4, 5));
            countList.add(qrrkStr.substring(3, 4));
            countList.add(qrrkStr.substring(2, 3));
            countList.add(qrrkStr.substring(1, 2));
            countList.add(qrrkStr.substring(0, 1));
        }
        return countList;
    }
/**
     * 打开违纪提交信息页面
     */
    @RequestMapping("/allWjTj")
    public ModelAndView allWj() {
        ModelAndView mv = new ModelAndView("/admin/all_wj_tj");
        List<UserWj> list = teacherService.userWjTj();
        mv.addObject("list",list);
        return mv;
    }

    @RequestMapping("/pass")
    public ModelAndView pass(UserWj userWj) {
        ModelAndView mv = new ModelAndView("/admin/all_wj_tj");
        String wjdj = userWj.getWjdj()+"";
        Map<String,String> wjMap = new HashMap<>(5);
        wjMap.put("1","口头警告");
        wjMap.put("2","警告");
        wjMap.put("3","严重警告");
        wjMap.put("4","记过");
        wjMap.put("5","记大过");
        teacherService.dePoint(userWj);
        teacherService.addWj(userWj);
        List<UserWj> list = teacherService.userWjTj();
        mv.addObject("list",list);
        return mv;
    }

    @RequestMapping("/cancel")
    public ModelAndView cancel(UserWj userWj) {
        ModelAndView mv = new ModelAndView("/admin/all_wj_tj");
        teacherService.delWjTj(userWj);
        List<UserWj> list = teacherService.userWjTj();
        mv.addObject("list",list);
        return mv;
    }

    @RequestMapping("/upTeam")
    public ModelAndView upTeam(UserWj userWj) {
        ModelAndView mv = new ModelAndView("/admin/week");
        adminService.upTeam();
        return mv;
    }

}
